package cn.dong.api.domain;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.io.Serializable;

@AllArgsConstructor
@NoArgsConstructor
@Data
@ToString
public class CommonResult<T> implements Serializable {

    /**
     * 消息
     */
    private String message;

    /**
     * 错误代码
     */
    private Integer code;

    /**
     * 数据
     */
    private T data;

    /**
     * 链式编程
     * @return
     */
    public static CommonResult ok(){
        return new CommonResult(ResultCodeEnum.SUCCESS.getMessage(), 20000,null);
    }

    public static CommonResult error(){
        return new CommonResult("失败!",40000,null);
    }

    public static CommonResult whether(boolean result) {
        if (result) {
            return new CommonResult("成功!",20000,null);
        }
        return new CommonResult("失败!",40000,null);
    }

    public CommonResult message(String message){
        this.message = message;
        return this;
    }

    public CommonResult code(Integer code){
        this.code = code;
        return this;
    }

    public CommonResult data(T data){
        this.data = data;
        return this;
    }


}
